//
//  ViewController.m
//  metalloader3D
//
//  Created by lrj on 2023/2/15.
//

#import "ViewController.h"
@import MetalKit;
#import "RenderBase3d.h"
#import "RenderBaseMVP3D.h"
#import "RenderLight.h"
#import "RenderSpecular.h"

@interface ViewController ()
{
    MTKView *_view;

    RenderBase3d *_renderer;
}
@end

@implementation ViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    self.view = [[MTKView alloc] initWithFrame:self.view.frame];
    return self;
}
//- (nullable instancetype)initWithCoder:(NSCoder *)coder{
//    self = [super initWithCoder:coder];
//    self.view = [[MTKView alloc] initWithFrame:self.view.frame];
//    return self;
//}



- (void)viewDidLoad {
    [super viewDidLoad];
}
- (IBAction)aaplemeshloader:(UIButton *)sender {
    _view = (MTKView *)self.view;

    _view.device = MTLCreateSystemDefaultDevice();
    _view.backgroundColor = UIColor.blackColor;
    if(!_view.device)
    {
        NSLog(@"Metal is not supported on this device");
        self.view = [[UIView alloc] initWithFrame:self.view.frame];
        return;
    }
    _renderer = [[RenderBase3d alloc] initWithMetalKitView:_view];

    [_renderer mtkView:_view drawableSizeWillChange:_view.bounds.size];
    
    _view.delegate = _renderer;
    
    
    
}

- (IBAction)mvpbtnAction:(UIButton *)sender {
    
    _renderer = nil;
    _view = (MTKView *)self.view;

    _view.device = MTLCreateSystemDefaultDevice();
    _view.backgroundColor = UIColor.blackColor;
    if(!_view.device)
    {
        NSLog(@"Metal is not supported on this device");
        self.view = [[UIView alloc] initWithFrame:self.view.frame];
        return;
    }
    _renderer = [[RenderBaseMVP3D alloc] initWithMetalKitView:_view];

    [_renderer mtkView:_view drawableSizeWillChange:_view.bounds.size];
    
    _view.delegate = _renderer;
    //RenderLight
    
}


- (IBAction)lightbtnAction:(UIButton *)sender {
    _view = (MTKView *)self.view;

    _view.device = MTLCreateSystemDefaultDevice();
    _view.backgroundColor = UIColor.redColor;
    if(!_view.device)
    {
        NSLog(@"Metal is not supported on this device");
        self.view = [[UIView alloc] initWithFrame:self.view.frame];
        return;
    }
    
    _renderer = [[RenderLight alloc] initWithMetalKitView:_view];

    [_renderer mtkView:_view drawableSizeWillChange:_view.bounds.size];
    
    _view.delegate = _renderer;
    
}


- (IBAction)specularBtnAction:(UIButton *)sender {
    _renderer = nil;
    _view = (MTKView *)self.view;

    _view.device = MTLCreateSystemDefaultDevice();
    _view.backgroundColor = UIColor.redColor;
    if(!_view.device)
    {
        NSLog(@"Metal is not supported on this device");
        self.view = [[UIView alloc] initWithFrame:self.view.frame];
        return;
    }
    
    _renderer = [[RenderSpecular alloc] initWithMetalKitView:_view];

    [_renderer mtkView:_view drawableSizeWillChange:_view.bounds.size];
    
    _view.delegate = _renderer;
}



@end
